package com.bdqn.tour.servlet.Hzy;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * packageName com.csdj.web
 *
 * @author winver
 * @version 1.0
 * @className ValSmsCodeServlet
 * @date 2024/12/17
 * @description TODO
 */
@WebServlet("/ValSmsCode")
public class ValSmsCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain;charset=utf-8");
        //获取手机号码
        String phone = req.getParameter("phone");
        String valCode = req.getParameter("valCode");

        // 如果请求参数中没有，再从session获取
        if (phone == null || phone.isEmpty()) {
            phone = (String) req.getSession().getAttribute("phone");
        }

        //会话中获取值 ，进行比较
        String  sessionValCode = (String)req.getSession().getAttribute(phone);

        PrintWriter out = resp.getWriter();
        Map<String,Object> map = new HashMap<>();
        //发送短信
        if (valCode.equals(sessionValCode)) {
            map.put("code","0000");
//            req.getSession().removeAttribute(phone);
        }else{
            map.put("code","2001");
        }
        //移除会话中的共享的code
        //转成字符串
        String json = JSON.toJSONString(map);
        out.print(json);
        out.close();

    }
}
